﻿using MySqlX.XDevAPI.Common;
using Newtonsoft.Json;
using NPOI.SS.Formula.Functions;
using NPOI.SS.Formula.PTG;
using RestSharp;
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.ComponentModel;
using System.Globalization;
using System.Linq;
using System.Linq.Expressions;
using System.Net.Http;
using System.Reflection.Metadata;
using System.Security.Policy;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Markup;
using static AutoTestCommunication_Service.Public;

namespace AutoTestCommunication_Service.Class
{

    internal class jiandaoyun
    {
        static RestClient client = new RestClient("http://192.168.80.180/prod-api/basicData/open-api/");
        
        public static (Data0, Data1) GetInformation(string SN)
        {
            try
            {
                //PN = ""; PRTI = ""; RLP = ""; PCBSN = ""; bengyuanSN = ""; PowerPN = "";
                string PCBSNO = ""; string 原始PRTI = "";string PRTI = "";string PCBSN = "";
                var request = new RestRequest("getProductionData", Method.GET);
                request.AddParameter("key", "MES_JGQ_BY");
                request.AddParameter("sn", SN);
                var respose = client.Execute(request);
                if (respose.StatusCode == System.Net.HttpStatusCode.OK)
                {
                    var result = JsonConvert.DeserializeObject<MesLaserSNResponse>(respose.Content);

                    var data0 = JsonConvert.DeserializeObject<Data0>(result.data[0].ToString());
                    var data1 = JsonConvert.DeserializeObject<Data1>(result.data[1].ToString());
                    //var data1 = (Data1)result.data[1];

                    int index = data1.SUPER_PRODUCT_TYPE.IndexOf("B");
                    if (index != -1)
                    { data1.SUPER_PRODUCT_TYPE = data1.SUPER_PRODUCT_TYPE.Substring(index, data1.SUPER_PRODUCT_TYPE.Count() - index); }
                    原始PRTI = data1.resistance_type;
                    PCBSNO = data1.control_panel_type;
                    //bengyuanSN = data1.SUPER_SN;
                    //RLP = data1.hgld_power;
                    //PowerPN = data1.switch_power_type;


                    //for (int i = 0; i < 原始PRTI.Length; i++)
                    //{
                    //    if (原始PRTI[i] >= '0' && 原始PRTI[i] <= '9')
                    //    {
                    //        PRTI = PRTI + 原始PRTI[i];
                    //    }
                    //}
                    //if (PRTI == null || PRTI.Length == 0)
                    //{
                    //    PRTI = "0";
                    //}
                    PRTI = "25";
                    data1.resistance_type = PRTI;

                    if (PCBSNO.Contains('-'))
                    {
                        string[] fruits = PCBSNO.Split('-');
                        PCBSN = fruits[3].Substring(2, 3);
                    }
                    else
                    {
                        for (int i = 0; i < PCBSNO.Length; i++)
                        {
                            if (Public.Seekfirstchar("数字", PCBSNO))
                            {
                                PCBSN = PCBSNO.Substring(0, 3);
                                break;
                            }
                            else
                            {
                                PCBSNO = PCBSNO.Substring(1, PCBSNO.Length - 1);
                            }
                        }
                    }
                    

                    data1.control_panel_type = PCBSN;
                    
                    //data1.hg_power = data1.hg_power.ToUpper();
                    //int ID = data1.hg_power.IndexOf("MW");
                    //data1.hg_power = data1.hg_power.Substring(0, ID);
                    data1.hg_power = "260";
                    if (Public.RLPCD.ContainsKey(data1.hg_power))
                    {
                        data1.hg_power = Public.RLPCD[data1.hg_power].ToString();
                    }
                    return (data0, data1);

                }
                else
                {
                    Data0 data0 = null; Data1 data1 = null;
                    return (data0, data1);
                }
            }
            catch (Exception ex)
            {
                TraceMethodInfo(ex.Message + ex.StackTrace);
                //PN = ""; PRTI = ""; RLP = ""; PCBSN = ""; bengyuanSN = ""; PowerPN = "";
                Data0 data0 = null;Data1 data1 = null;
                return (data0, data1);
            }
        }

        public static bool GetInformation1(string SN, out string PN, out string PRTI, out string RLP, out string PCBSN, out string bengyuanSN,out string PowerPN)
        {
            try
            {
                PN = ""; PRTI = ""; RLP = ""; PCBSN = ""; bengyuanSN = ""; PowerPN = "";
                var client = new RestClient("https://api.jiandaoyun.com/api/v4/app/5e201bfe0715680006b9e82d/entry/62625acbde6db4000ac9c49b/data");
                //client.Timeout = -1;
                var request = new RestRequest(Method.POST);
                request.AddHeader("Authorization", "Bearer Ue3NWkomm5p9d9KleHssvMzfGCL33l3p");
                request.AddHeader("Content-Type", "application/json");
                request.AddHeader("Cookie", "_csrf=s%3A-MboxWaeasc5ivmBCkWcA3jK.GoRv26RtU6MwHLD4ob%2FHwdKCfOruFMzd58OoJDfKXA8");
                request.AddParameter("application/json", "{\r\n    \"limit\": 1,\r\n    \"fields\": [\"_widget_1579163533820\",\"_widget_1651889643442\",\"_widget_1636015236938\",\"_widget_1589606291059\"],\r\n    \"filter\": {\r\n                \"cond\": [\r\n                            {\r\n                                \"field\": \"_widget_1579163533839\",\r\n                                \"method\": \"eq\",\r\n                                \"value\": \"" + SN + "\"\r\n                            }\r\n                        ]\r\n                }\r\n}", ParameterType.RequestBody);
                IRestResponse response = client.Execute(request);
                //Console.WriteLine(response.Content);
                string factMessage = response.Content;
                string[] yunArray = factMessage.Split(new string[] { ",", ":" }, StringSplitOptions.RemoveEmptyEntries);
                char[] charsToTrim = { '"', ' ', '\'' };
                string PCBSNO = ""; string 原始PRTI = "";
                for (int i = 0; i < yunArray.Count(); i++)
                {
                    if (yunArray[i].Contains("_widget_1579163533820"))
                    {
                        PN = yunArray[i + 1].Trim(charsToTrim);
                    }
                    if (yunArray[i].Contains("_widget_1636015238558"))
                    {
                        原始PRTI = yunArray[i + 1].Trim(charsToTrim);
                    }
                    if (yunArray[i].Contains("_widget_1650613044351"))
                    {
                        PCBSNO = yunArray[i + 1].Trim(charsToTrim);
                    }
                    if (yunArray[i].Contains("_widget_1651889644167"))
                    {
                        bengyuanSN = yunArray[i + 1].Trim(charsToTrim).Substring(0, 10);
                    }
                    if (yunArray[i].Contains("_widget_1650784572876"))
                    {
                        RLP = yunArray[i + 1].Trim(charsToTrim);
                    }
                    if (yunArray[i].Contains("_widget_1636015237204"))
                    {
                        PowerPN = yunArray[i + 1].Trim(charsToTrim);
                    }
                    
                }
                for (int i = 0; i < 原始PRTI.Length; i++)
                {
                    if (原始PRTI[i] >= '0' && 原始PRTI[i] <= '9')
                    {
                        PRTI = PRTI + 原始PRTI[i];
                    }
                }

                for (int i = 0; i < PCBSNO.Length; i++)
                {
                    if (Public.Seekfirstchar("数字", PCBSNO))
                    {
                        PCBSN = PCBSNO.Substring(0, 3);
                        break;
                    }
                    else
                    {
                        PCBSNO = PCBSNO.Substring(1, PCBSNO.Length - 1);
                    }
                }

                RLP= RLP.ToUpper();
                int ID = RLP.IndexOf("MW");
                RLP = RLP.Substring(0, ID);
                if (Public.RLPCD.ContainsKey(RLP))
                {
                    RLP = Public.RLPCD[RLP].ToString();
                }

                return true;
            }
            catch (Exception ex)
            {
                 TraceMethodInfo(ex.Message+ex.StackTrace);
                PN = ""; PRTI = ""; RLP = ""; PCBSN = ""; bengyuanSN = ""; PowerPN="";
                return false;
            }

        }
        public static void GETIP()
        {
            string SN = "FMNG017359";
             SN = "BFL-CW2000T-A000005";
            //PN = ""; PRTI = ""; RLP = ""; PCBSN = ""; bengyuanSN = "";
            var client = new RestClient("https://api.jiandaoyun.com/api/v4/app/5e201bfe0715680006b9e82d/entry/62625acbde6db4000ac9c49b/data");
            //client.Timeout = -1;
            var request = new RestRequest(Method.POST);
            request.AddHeader("Authorization", "Bearer Ue3NWkomm5p9d9KleHssvMzfGCL33l3p");
            request.AddHeader("Content-Type", "application/json");
            request.AddHeader("Cookie", "_csrf=s%3A-MboxWaeasc5ivmBCkWcA3jK.GoRv26RtU6MwHLD4ob%2FHwdKCfOruFMzd58OoJDfKXA8");
            request.AddParameter("application/json", "{\r\n    \"limit\": 1,\r\n    \"fields\": [\"_widget_1579163533820\",\"_widget_1651889643442\",\"_widget_1636015236938\",\"_widget_1589606291059\"],\r\n    \"filter\": {\r\n                \"cond\": [\r\n                            {\r\n                                \"field\": \"_widget_1579163533839\",\r\n                                \"method\": \"eq\",\r\n                                \"value\": \"" + SN + "\"\r\n                            }\r\n                        ]\r\n                }\r\n}", ParameterType.RequestBody);
            IRestResponse response = client.Execute(request);
            //Console.WriteLine(response.Content);
            string factMessage = response.Content;
            string[] yunArray = factMessage.Split(new string[] { ",", ":" }, StringSplitOptions.RemoveEmptyEntries);
            char[] charsToTrim = { '"', ' ', '\'' };
            string PN = yunArray[2].Trim(charsToTrim);
            string PRTI = yunArray[56].Trim(charsToTrim).Substring(0, 2);
            string PCBSNO = yunArray[58].Trim(charsToTrim);
            string PCBSN;
            for (int i = 0; i < PCBSNO.Length; i++)
            {
                if (Public.Seekfirstchar("数字", PCBSNO))
                {
                    PCBSN = PCBSNO.Substring(0, 3);
                    break;
                }
                else
                {
                    PCBSNO = PCBSNO.Substring(1, PCBSNO.Length - 1);
                }
            }
            string RLP = yunArray[85].Trim(charsToTrim);
            string bengyuanSN = yunArray[17].Trim(charsToTrim).Substring(0, 10);
            int ID = RLP.IndexOf("mW");
            RLP = RLP.Substring(0, ID);
            if (Public.RLPCD.ContainsKey(RLP))
            {
                RLP = Public.RLPCD[RLP].ToString();
            }
        }
    }
}
